package com.xiaoha.reading.controller;

import com.xiaoha.reading.entity.Book;
import com.xiaoha.reading.jpa.BookJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookingController {

    @Autowired
    private BookJpa bookJpa;

    @RequestMapping(value = "/{reader}",method = RequestMethod.GET)
    private List<Book> getByReader(@PathVariable("reader") String reader){
        List<Book> books = bookJpa.getByReader(reader);
        return books;
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public List<Book> getAll(){
        return bookJpa.findAll();
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    private Book saveBook(@Validated @RequestBody Book book){
        Book save = bookJpa.save(book);
        return save;
    }


}
